<?php

declare(strict_types=1);

namespace App\CustHub\Listener;

use App\CustHub\Constants\ContactBehaviorTypeConstants;
use App\CustHub\Constants\ContactTaskCenterTyperConstants;
use App\CustHub\Event\ContactOperationBehaviorAfter;
use App\CustHub\Event\ContactTaskCenterAfter;
use App\CustHub\Mapper\ChCorpTagMapper;
use App\CustHub\Mapper\ChSmartTagMapper;
use App\CustHub\Model\ChContactTaskCenter;
use App\CustHub\Service\ChContactService;
use App\CustHub\Service\ChSmartAutoPushService;
use App\CustHub\Service\ChSmartPushSchemeService;
use App\CustHub\Service\ChSopService;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Psr\Container\ContainerInterface;
use function Hyperf\Collection\collect;
use function Hyperf\Coroutine\co;

/**
 * 客户任务中心事件监听
 */
#[Listener]
class ContactTaskCenterListener implements ListenerInterface
{

    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function listen(): array
    {
        return [
            ContactTaskCenterAfter::class,
        ];
    }

    public function process(object $event): void
    {
        /** @var ChContactTaskCenter $taskInfo */
        $taskInfo = $event->taskInfo;

        $contactIds = $taskInfo->content['contactIds'];
        $tagIds = $taskInfo->content['tagIds'];
        $removeTagIds = $taskInfo->content['removeTagIds'];

        $contactList = collect($event->contactList);

        switch ($taskInfo->type){
            case ContactTaskCenterTyperConstants::PUT_ON_SMART_TAG:
                //打上智能标签
                //自动推送
                container()->get(ChSmartAutoPushService::class)->getMeetTheConditionScheme($taskInfo['uid'],$taskInfo['main_uid'],$contactIds,$tagIds);
                break;
            case ContactTaskCenterTyperConstants::DELETE_SMART_TAG:
                //删除智能标签
                break;
            case ContactTaskCenterTyperConstants::MOVE_SMART_TAG:
                //移动智能标签
                //自动推送
                container()->get(ChSmartAutoPushService::class)->getMeetTheConditionScheme($taskInfo['uid'],$taskInfo['main_uid'],$contactIds,$tagIds);
                break;
            case ContactTaskCenterTyperConstants::PUT_ON_CORP_TAG:
                //打上企业标签
                break;
            case ContactTaskCenterTyperConstants::DELETE_CORP_TAG:
                //删除企业标签
                break;
            case ContactTaskCenterTyperConstants::MOVE_CORP_TAG:
                //移动企业标签
                break;
        }

        $robotIds = $contactList->pluck('robot_id')->unique()->toArray();
        switch ($taskInfo->type){
            case ContactTaskCenterTyperConstants::PUT_ON_SMART_TAG:
            case ContactTaskCenterTyperConstants::DELETE_SMART_TAG:
            case ContactTaskCenterTyperConstants::MOVE_SMART_TAG:
            case ContactTaskCenterTyperConstants::PUT_ON_CORP_TAG:
            case ContactTaskCenterTyperConstants::DELETE_CORP_TAG:
            case ContactTaskCenterTyperConstants::MOVE_CORP_TAG:
                $this->triggerScheme($robotIds,$contactIds);
                break;
        }
    }

    private function triggerScheme(array $robotIds,array $contactIds): void
    {
        $this->container->get(ChSmartPushSchemeService::class)->getMeetTheConditionScheme($robotIds,$contactIds);
    }
}